package org.example.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.DepartmentDto;
import org.example.dto.TeacherDto;
import org.example.service.TeacherService;
import org.example.service.TestService;
import org.example.utils.JsonUtils;
import org.example.vo.DepartmentVo;
import org.example.vo.TeacherVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rongchang Wu
 * @version 1.0
 * @description: TODO
 * @date 2021/12/9 20:41
 */
@RestController
public class TeacherController {
    @Resource
    TeacherService service;

    @GetMapping("/getTeacherByName")
    @ResponseBody
    public void getTeacherByName(HttpServletResponse resp, String teacherName) throws IOException {
        TeacherDto teacher = service.queryTeacherByName(teacherName);
        //以上service根据系别名查找，返回对应的系
        List<TeacherDto> teacherList = new ArrayList<>();
        teacherList.add(teacher);

        JsonUtils.customJson(resp, teacherList, teacherList.size());
    }

    @GetMapping("/getTeacherList")
    @ResponseBody
    public void getTeacherList(HttpServletResponse resp, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<TeacherDto> teacherList = service.queryAllTeacher();
        for (int i = 0; i < teacherList.size(); i++) {
            System.out.println(teacherList.get(i).toString());
            TeacherDto teacher = teacherList.get(i);
            teacher.setAge(DateUtil.ageOfNow(teacher.getBirthday()));
            teacher.setUnionAddress(teacher.getProvince() + teacher.getCity() + teacher.getDistrict() + teacher.getAddress());
        }
        Integer count = service.countTeacher();
        //以上调用service查找所有系别，赋值给teacherList；查找系别数量，赋值给count

        JsonUtils.customJson(resp, teacherList, count);
    }

    @GetMapping(value = "/deleteTeacher", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String deleteTeacher(String teacherId) {
        String[] teacherIds = teacherId.split(",");
        Integer status = 0;
        for (int i = 0; i < teacherIds.length; i++) {
            status += service.deleteTeacher(Integer.parseInt(teacherIds[i]));
        }
        //以上调用service方法删除系别

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/addTeacher", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String addTeacher(TeacherVo teacher) {
        Integer status = service.addTeacher(teacher);
        //以上调用service方法添加系别

        return status > 0 ? "success" : "error";
    }

    @GetMapping("/getTeacherById")
    @ResponseBody
    public TeacherDto getTeacherById(Integer teacherId) {
        TeacherDto teacher = service.queryTeacherById(teacherId);
        //以上service根据系系号查找，返回对应的系

        return teacher;
    }

    @GetMapping("/editTeacher")
    @ResponseBody
    public String editTeacher(TeacherVo teacher) {
        Integer status = service.updateTeacher(teacher);
        //以上调用service方法修改系别

        return status > 0 ? "success" : "error";
    }
}
